package com.innovation.chengzhangriji.ui.widget;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.innovation.chengzhangriji.R;

public class SelectRoleLayout extends RelativeLayout implements View.OnClickListener {

    private static final int WIDTH_PADDING = 24;

    private static final int HEIGHT = 320;

    private CuteTextView mCtvHint;

    private CuteTextView mCtvText;

    private CharSequence[] roleArray;

    private int maxLength;

    public SelectRoleLayout(Context context) {
        super(context);
        init(context, null);
    }

    public SelectRoleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public SelectRoleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @Override
    public void onClick(View v) {
        onCreateDialog(mCtvHint.getText(), mCtvText.getText());
    }

    public void setCuteHint(String hint) {
        mCtvHint.setText(hint);
    }

    public void setRoleArray(CharSequence[] charSequences) {
        roleArray = charSequences;
    }

    public void setText(CharSequence charSequence) {
        mCtvText.setText(charSequence);
    }

    public String getText() {
        return mCtvText.getText().toString();
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SelectRoleLayout);
        String selectRoleHint = typedArray.getString(R.styleable.SelectRoleLayout_select_role_hint);
        int hintColor = typedArray.getColor(R.styleable.SelectRoleLayout_select_role_hint_color, getResources().getColor(android.R.color.darker_gray));
        int textColor = typedArray.getColor(R.styleable.SelectRoleLayout_select_role_array_color, getResources().getColor(android.R.color.black));
        CharSequence[] selectRoleArray = typedArray.getTextArray(R.styleable.SelectRoleLayout_select_role_array);
        maxLength = typedArray.getInteger(R.styleable.SelectRoleLayout_select_role_max_length, -1);
        typedArray.recycle();

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.layout_select, this);
        mCtvHint = (CuteTextView) view.findViewById(R.id.cute_text_view_hint);
        mCtvHint.setTextColor(hintColor);
        mCtvText = (CuteTextView) view.findViewById(R.id.cute_text_view_text);
        mCtvText.setTextColor(textColor);
        setOnClickListener(this);

        if (!TextUtils.isEmpty(selectRoleHint)) {
            mCtvHint.setText(selectRoleHint);
        }
        if (selectRoleArray != null && selectRoleArray.length > 0) {
            roleArray = selectRoleArray;
            mCtvText.setText(selectRoleArray[0]);
        }
    }

    private void onCreateDialog(CharSequence title, CharSequence content) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_select_role, null);
        final CuteTextView cuteTitle = (CuteTextView) view.findViewById(R.id.role_title);
        final CuteMiniEditText cuteName = (CuteMiniEditText) view.findViewById(R.id.role_name);
        if (maxLength != -1) {
            cuteName.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
        }
        final ListView listView = (ListView) view.findViewById(R.id.role_list);
        listView.setAdapter(new RoleAdapter());
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cuteName.setText(roleArray[position]);
            }
        });
        cuteTitle.setText(title);
        cuteName.setText(content);
        builder.setView(view);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mCtvText.setText(cuteName.getText());
            }
        });
        builder.setNegativeButton(android.R.string.cancel, null);
        AlertDialog dialog = builder.create();
        dialog.show();

        if (roleArray != null && roleArray.length <= 1) {
            listView.setVisibility(View.GONE);
        } else {
            DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
            int width = (int) (dm.widthPixels - WIDTH_PADDING * 2 * dm.density);
            int height = (int) (HEIGHT * dm.density);
            dialog.getWindow().setLayout(width, height);
        }

    }

    private class RoleAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            if (roleArray != null && roleArray.length > 0) {
                return roleArray.length;
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.list_item_role, null);
                viewHolder.cuteTextView = (CuteTextView) convertView.findViewById(R.id.role);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.cuteTextView.setText(roleArray[position]);
            return convertView;
        }

        private class ViewHolder {
            CuteTextView cuteTextView;
        }

    }
}
